<?php
// 连接数据库
include_once('./config/config.php');

// 获取操作 action
$action = $_POST['action'] ?? '';
// 获取管理员用户名操作select_admin_username
if ($action === 'select_admin_username') {
  $username = isset($_POST['username']) ? trim($_POST['username']) : '';
  $sql = "SELECT * FROM `pre_admin` WHERE `username` = '$username'";
  $admin = find($sql);
  if ($admin) {
    echo json_encode(['code' => 0, 'msg' => '用户名已存在', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 1, 'msg' => '用户名可用', 'data' => null]);
    exit;
  }
}

// 获取部门操作select_department 
if ($action === 'select_department') {
  $sql = "SELECT * FROM `pre_department`";
  $res = all($sql);
  if ($res) {
    echo json_encode(['code' => 1, 'msg' => '查询部门成功', 'data' => $res]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '查询部门失败', 'data' => $res]);
    exit;
  }
}

// 获取职位操作select_job
if ($action === 'select_job') {
  $depid = $_POST['depid'] ?? 0;
  $sql = "SELECT * FROM `pre_job` WHERE `depid` = $depid";
  $res = all($sql);
  if ($res) {
    echo json_encode(['code' => 1, 'msg' => '查询职位成功', 'data' => $res]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '数据为空或查询失败', 'data' => $res]);
    exit;
  }
}

// 获取部门操作check_dep_name
if ($action === 'check_dep_name') {
  $dep = isset($_POST['dep']) ? trim($_POST['dep']) : '';
  $sql = "SELECT * FROM `pre_department` WHERE `name` = '$dep'";
  $res = find($sql);
  if ($res) {
    echo json_encode(['code' => 0, 'msg' => '部门已存在', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 1, 'msg' => '部门名称可用', 'data' => null]);
    exit;
  }
}

// 获取职位操作check_job_name
if ($action === 'check_job_name') {
  $job = isset($_POST['job']) ? trim($_POST['job']) : '';
  $depid = intval($_POST['depid']) ?? 0;
  $sql = "SELECT * FROM `pre_job` WHERE `depid` = $depid AND `name` = '$job'";
  $res = find($sql);
  if ($res) {
    echo json_encode(['code' => 0, 'msg' => '职位已存在', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 1, 'msg' => '职位名称可用', 'data' => null]);
    exit;
  }
}

/* // 获取全部职位操作select_all_job
if ($action === 'select_all_job') {
  $limit = $_POST['limit'];
  $page = $_POST['page'] ?? 1;
  $start = ($page - 1) * $limit;
  // $count = "SELECT COUNT(*) FROM `pre_job`";

  $sql = "SELECT `j` . *, `d` . `name` AS `dname` FROM `pre_job` AS `j` LEFT JOIN `pre_department` AS `d` ON `j` . `depid` = `d` . `id` ORDER BY `j` . `id` LIMIT $start,$limit";
  $data = all($sql);
  if ($data) {
    echo json_encode(['code' => 1, 'msg' => '查询职位数据成功', 'data' => $data]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '查询职位数据失败', 'data' => null]);
    exit;
  }
}
 */

// 删除员工操作person_delete
if ($action === 'person_delete') {
  $id = $_POST['id'];
  // 查询一下这个id是否存在
  $sql = "SELECT * FROM `pre_person` WHERE `id` = $id";
  $res = find($sql);
  if (!$res) {
    echo json_encode(['code' => 0, 'msg' => '员工不存在', 'data' => null]);
    exit;
  }
  $result = del('person', "`id`=$id");
  if ($result) {
    // 头像也要删除
    @is_file('.' . $res['avatar'] && unlink('.' . $res['avatar']));
    echo json_encode(['code' => 1, 'msg' => '删除员工成功', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '删除员工失败', 'data' => null]);
    exit;
  }
}

// 删除管理员操作delete_admin
if ($action === 'delete_admin') {
  $id = $_POST['id'];
  // 查询一下这个id是否存在
  $sql = "SELECT * FROM `pre_admin` WHERE `id` = $id";
  $res = find($sql);
  if (!$res) {
    echo json_encode(['code' => 0, 'msg' => '管理员不存在', 'data' => null]);
    exit;
  }
  $result = del('admin', "`id`=$id");
  if ($result) {
    // 头像也要删除
    @is_file('.' . $res['avatar'] && unlink('.' . $res['avatar']));
    echo json_encode(['code' => 1, 'msg' => '删除管理员成功', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '删除管理员失败', 'data' => null]);
    exit;
  }
}

// 删除部门操作delete_dep
if ($action === 'delete_dep') {
  $id = $_POST['id'];
  // 查询一下这个id是否存在
  $sql = "SELECT * FROM `pre_department` WHERE `id` = $id";
  $res = find($sql);
  if (!$res) {
    echo json_encode(['code' => 0, 'msg' => '部门不存在', 'data' => null]);
    exit;
  }
  $result = del('department', "`id`=$id");

  if ($result) {
    echo json_encode(['code' => 1, 'msg' => '删除部门成功', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '删除部门失败', 'data' => null]);
    exit;
  }
}

// 删除职位操作delete_job
if ($action === 'delete_job') {
  $id = $_POST['id'];
  // 查询一下这个id是否存在
  $sql = "SELECT * FROM `pre_job` WHERE `id` = $id";
  $res = find($sql);
  if (!$res) {
    echo json_encode(['code' => 0, 'msg' => '职位不存在', 'data' => null]);
    exit;
  }
  $result = del('job', "`id`=$id");

  if ($result) {
    echo json_encode(['code' => 1, 'msg' => '删除职位成功', 'data' => null]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '删除职位失败', 'data' => null]);
    exit;
  }
}

/* // 员工查询search_person
if ($action === 'search_person') {
  // 接收要查询的值
  $search = $_POST['ipt'] ?? '';
  $sql = "SELECT * FROM `pre_person` WHERE `name` LIKE '%$search%'";
  $res = all($sql);
  if ($res) {
    echo json_encode(['code' => 1, 'msg' => '查询员工数据成功', 'data' => $res]);
    exit;
  } else {
    echo json_encode(['code' => 0, 'msg' => '查询员工数据失败', 'data' => null]);
    exit;
  }
}
 */

// wx接口
if ($action === 'wx') {
  $code = $_POST['code'] ?? '';

  if (!$code) {
    echo json_encode(['code' => 0, 'msg' => '临时登录凭证不存在', 'data' => null]);
    exit;
  }

  $ApiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wxe78e1a6565db6c62&secret=6999d59dc9e4baabc487d5b7e27b5225&js_code=$code&grant_type=authorization_code";

  $res = HttpRequest($ApiUrl);

  $data = json_decode($res, true);

  $openid = $data['openid'] ?? '';

  if (empty($openid)) {
    echo json_encode(['code' => 0, 'msg' => '获取登录凭证失败', 'data' => null]);
    exit;
  }

  $sql = "SELECT * FROM `pre_admin` WHERE `openid` = '$openid'";

  $admin = find($sql);

  // 如果根据openid找不到用户，那么把openid返回前端，然后前端跳转到绑定账号页面，让用户输入用户名和密码
  // if(!$admin)
  // {
  //     echo json_encode(['code' => 0,'msg' => '未绑定账号','data' => ['openid' => $openid]]);
  //     exit;
  // }

  // 直接写死绑定admin这个用户名
  if ($admin) {
    unset($admin['salt']);
    unset($admin['password']);

    echo json_encode(['code' => 1, 'msg' => '登录成功', 'data' => $admin]);
    exit;
  } else {
    // 模拟绑定账号业务

    $sql = "SELECT * FROM `pre_admin` WHERE `username` = 'admin'";

    $admin = find($sql);

    if (!$admin) {
      echo json_encode(['code' => 0, 'msg' => '用户名不存在', 'data' => null]);
      exit;
    }

    $data = [
      'openid' => $openid
    ];

    $adminid = $admin['id'] ?? 0;

    $result = update('admin', $data, "`id` = '$adminid'");

    if ($result === true) {
      $sql = "SELECT * FROM `pre_admin` WHERE `openid` = '$openid'";

      $admin = find($sql);

      unset($admin['salt']);
      unset($admin['password']);

      echo json_encode(['code' => 1, 'msg' => '登录成功', 'data' => $admin]);
      exit;
    } else {
      echo json_encode(['code' => 0, 'msg' => '登录失败', 'data' => null]);
      exit;
    }
  }
}
